import { Dialog } from 'vant'
import jsCookie from 'js-cookie'
import CryptoJS from 'crypto-js'
import { API_SIGN } from '@/common/config'

/**
 * 提示消息
 * @param msg
 */
export function showMsg (msg) {
  Dialog.alert({
    title: '提示消息',
    message: msg
  }).then(() => {})
}

/**
 * 出错提示
 * @param msg
 */
export function showError (msg = '网络出错了，请刷新重试') {
  showMsg(msg)
}

/**
 * 数字转字母
 * @param num
 * @returns {string}
 */
export function num2Char (num = 0) {
  return String.fromCharCode(65 + parseInt(num))
}

/**
 * 字母转数字
 * @param char
 * @returns {number}
 */
export function char2Num (char) {
  return char.toUpperCase().charCodeAt()
}

/**
 * 获取当前H5的链接
 * @returns {string}
 */
export function getH5URL () {
  if (process.env.VUE_APP_ROUTER) {
    return window.location.origin + process.env.VUE_APP_ROUTER
  } else {
    return window.location.href.split('#')[0]
  }
}

/**
 * 删除所有cookie
 */
export function removeAllCookie () {
  const all = jsCookie.get() // 获取所有的cookie
  for (const key in all) {
    jsCookie.remove(key) // 删除cookie
  }
}

/**
 * 获取签名
 * @param data
 * @param timestamp
 * @returns {*}
 */
export function getSign (data = {}, timestamp = 0) {
  if (!timestamp) {
    timestamp = Math.floor((new Date().getTime()) / 1000)
  }
  const signData = JSON.stringify(data) === '{}' ? '' : JSON.stringify(data)
  const sign = CryptoJS.MD5(signData + timestamp + API_SIGN).toString()
  return {
    data: signData,
    sign,
    timestamp
  }
}
